package com.vancouver.knows.ui.activity.my;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;
import com.pangu.util.StringUtil;
import com.vancouver.knows.R;
import com.vancouver.knows.business.task.mark.ResetPasswordTaskMark;
import com.vancouver.knows.ui.base.BaseActivity;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Chris Li on 2016-07-26.
 */
public class ResetPasswordActivity extends BaseActivity implements IResultReceiver {

    @Bind(R.id.old_password_input)
    EditText mOldPasswordInput;
    @Bind(R.id.new_password_input)
    EditText mNewPasswordInput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password);
    }

    @OnClick(R.id.back_icon)
    public void onBackButtonClicked() {
        onBackPressed();
    }

    @OnClick(R.id.go_button)
    public void onGoButtonClicked() {

        String oldPassword = mOldPasswordInput.getText().toString().trim();
        String newPassword = mNewPasswordInput.getText().toString().trim();
        String userId = imContext.getSharedPrefManager().getUserId();

        if (StringUtil.isEmptyString(oldPassword) || StringUtil.isEmptyString(newPassword)) {
            return;
        }

        if (userId == null) {
            return;
        }

        ResetPasswordTaskMark resetPasswordTaskMark = imContext.getBusinessModule().getTaskMarkPool().getResetPasswordTaskMark();
        if (resetPasswordTaskMark.getTaskStatus() == ATaskMark.HANDLE_DOING) {
            return;
        }

        imContext.getBusinessModule().getServiceWrapper().resetPassword(this, resetPasswordTaskMark, userId, oldPassword, newPassword);
    }

    @Override
    public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
        if (exception == null) {
            Toast.makeText(this, R.string.password_reset_success, Toast.LENGTH_SHORT).show();
            finish();
        } else {
            mOldPasswordInput.setText("");
            mNewPasswordInput.setText("");
            Toast.makeText(this, exception.getExMessage(), Toast.LENGTH_SHORT).show();
        }
    }
}
